;(function(){
    class Register {
        constructor() {
            this.user = document.getElementById('user');
            this.pass = document.getElementById('pass');
            this.passSure = document.getElementById('passtwo');
            this.btn = document.querySelector('.btn');
            this.tel = document.getElementById('tel');
            this.email = document.getElementById('email');
            this.rem=document.getElementById('remember');
    
            this.reg();
            this.addEvent();
        }
        reg() {
            let that = this;
            let user = false;
            // 用户名的正则
            this.user.oninput = function () {
                let reg = /^[a-z][\w\-]{6,20}$/i; // a-z 数字字母下划线- 6到20位 忽略大小写
                if (reg.test(this.value)) {
                    this.nextElementSibling.innerHTML = "验证通过";
                    this.nextElementSibling.style.display = 'block';
                    user = true;
                } else {
                    this.nextElementSibling.innerHTML = "未通过";
                    this.nextElementSibling.style.display = 'block';
                    user = false;
                }
            }
            // 密码的正则
            let pass = false;
            let passSure = false;
            this.pass.oninput = function () {
                var numReg = /\d/;
                var azReg = /[a-z]/i;
                var tsReg = /[^a-z\d]/i;
                var a = 0;
                var b = 0;
                var c = 0;
                // 判断是否出现
                if (numReg.test(this.value)) {
                    a = 1
                }
                if (azReg.test(this.value)) {
                    b = 1
                }
                if (tsReg.test(this.value)) {
                    c = 1
                }
                // 通过累加状态,判断出现了几种
                var msg = "";
                switch (a + b + c) {
                    case 1:
                        msg = "简单";
                        break;
                    case 2:
                        msg = "一般";
                        break;
                    case 3:
                        msg = "困难";
                        break;
                }
                // 设置提示信息
                this.nextElementSibling.innerHTML = msg;
                this.nextElementSibling.style.display = 'block'
    
                // 修改密码是否成功状态
                pass = true;
    
                // 在密码验证中，增加重复密码验证
                if (that.passSure.value == "") return;
                if (this.value === that.passSure.value) {
                    that.passSure.nextElementSibling.innerHTML = "密码一致";
                    that.passSure.nextElementSibling.style.display = 'block';
                    passSure = true;
                } else {
                    that.passSure.nextElementSibling.innerHTML = "密码不一致";
                    that.passSure.nextElementSibling.style.display = 'block';
                    passSure = false;
                }
            }
            this.passSure.oninput = function () {
                // 判断密码是否重复
                if (this.value === that.pass.value) {
                    this.nextElementSibling.innerHTML = "密码一致";
                    this.nextElementSibling.style.display='block';
                    passSure = true
                } else {
                    this.nextElementSibling.innerHTML = "密码不一致";
                    this.nextElementSibling.style.display='block';
                    passSure = false
                }
            }
            // 手机号的正则
            let phone = false;
            this.tel.oninput = function () {
                let phoneReg = /^1[3456789]\d{9}$/;
                if (phoneReg.test(this.value)) {
                    this.nextElementSibling.innerHTML = "验证通过";
                    this.nextElementSibling.style.display = 'block';
                    phone = true;
                } else {
                    this.nextElementSibling.innerHTML = "未通过";
                    this.nextElementSibling.style.display = 'block';
                    phone = false;
                }
            }
            let email=false;
            // 邮箱验证
            this.email.oninput = function () {
                let emailReg = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
                if (emailReg.test(this.value)) {
                    this.nextElementSibling.innerHTML = "验证通过";
                    this.nextElementSibling.style.display = 'block';
                    email = true;
                } else {
                    this.nextElementSibling.innerHTML = "未通过";
                    this.nextElementSibling.style.display = 'block';
                    email = false;
                }
            }
        }
        addEvent() {
            let that = this;
            this.btn.onclick = function () {
                that.u = that.user.value;
                that.p = that.pass.value;
                that.pS = that.passSure.value;
                that.t = that.tel.value;
                that.e = that.email.value;
                that.setUser();
            }
        }
        setUser() {
            this.userMsg = localStorage.getItem('userMsg') ? JSON.parse(localStorage.getItem('userMsg')) : [];
    
            let res = this.userMsg.some(val => {
                return val.user === this.u;
            });
            if (res) {
                // this.u='用户名重复';
                alert('用户名重复')
    
            } else {
                this.userMsg.push({
                    user: this.u,
                    pass: this.p,
                    tel:this.tel,
                    email:this.email,
                    onoff: 0
                });
            }
            if(this.rem.checked){
            localStorage.setItem('userMsg', JSON.stringify(this.userMsg));
            this.btn.value='即将跳转到登录'
    
            }
            setTimeout(() => {
                location.href = 'http://localhost/blShop-gulp/src/login.html';
            }, 3000);
        }
    
    }
    new Register();
})();